/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.mzk.oai.metadataRepository.cli;

import cz.mzk.oai.metadataRepository.repository.Repository;
import cz.mzk.oai.metadataRepository.repository.collections.MetadataCollection;

/**
 *
 * @author Martin Řehánek (rehan at mzk.cz)
 */
public class CollectionParser {

    public static final String ALL_COLLECTIONS = "ALL";
    private final Repository repository;
    private boolean allCollections = false;
    private MetadataCollection collection;

    /**
     *
     * @param collectionName name of collection or ALL if all collections should be processd
     */
    public CollectionParser(Repository repository, String collectionName) throws Exception {
        this.repository = repository;
        determineCollection(collectionName);
    }

    private void determineCollection(String collectionName) throws Exception {
        if (ALL_COLLECTIONS.equals(collectionName)) {
            allCollections = true;
            return;
        } else {
            findCollection(collectionName);
        }
    }

    private void findCollection(String collectionName) throws Exception {
        MetadataCollection foundCollection = repository.findCollection(collectionName);
        if (foundCollection == null) {
            throw new Exception("No such collection " + collectionName);
        } else {
            collection = foundCollection;
        }
    }

    /**
     * @return the collectionToAnalyze
     */
    public MetadataCollection getCollection() {
        return collection;
    }

    public boolean allCollections() {
        return allCollections;
    }
}
